package swing;

import app.Main;
import packages.db.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import static app.Main.viewChange;

public class AddView extends JPanel{
    public JPanel JPanel0;
    private JTextField NameField;
    private JTextField PhoneField;
    private JTextField EmailField;
    private JTextField NoteField;
    private JButton Button1;
    private JLabel NameLabel;
    private JLabel CategoryLabel;
    private JLabel ColorLabel;
    private JLabel LocationLabel;
    private JButton Button2;
    private JPanel JPanel2;
    private JPanel JPanel1;
    private JPanel JPanel3;
    private JLabel TitleLabel;
    private JComboBox comboBox1;
    private JComboBox comboBox2;
    private JComboBox comboBox3;
    private JComboBox comboBox4;
    private JLabel PersonalityLabel;
    ArrayList<String> catLocation = new ArrayList<>();
    ArrayList<String> catCategory = new ArrayList<>();
    ArrayList<String> catColor = new ArrayList<>();
    ArrayList<String> catPersonality = new ArrayList<>();





    public AddView() throws IOException {
        Font font=new Font("楷体", Font.BOLD,30);
        TitleLabel.setFont(font);

        font = new Font("楷体", Font.BOLD,20);
        Button1.setFont(font);
        Button2.setFont(font);
        CategoryLabel.setFont(font);
        NameLabel.setFont(font);
        PersonalityLabel.setFont(font);
        LocationLabel.setFont(font);
        ColorLabel.setFont(font);

        Color color = new Color(0xff, 0xff, 0xff);
        Button1.setBackground(color);
        Button2.setBackground(color);

        JPanel0.setOpaque(false);
        JPanel1.setOpaque(false);
        JPanel2.setOpaque(false);
        JPanel3.setOpaque(false);


        // 查数据库获取所有位置
        DBUtil db = new DBUtil();
        Object[] objs = {};
        ResultSet set = db.select("select * from location", objs);
        try {
            while (set.next()) {
                catLocation.add(set.getString("location"));
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }

        for(String str : catLocation){
            comboBox3.addItem(str);
        }


        // 查数据库获取所有颜色
        set = db.select("select * from color", objs);
        try {
            while (set.next()) {
                catColor.add(set.getString("color"));
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }

        for(String str : catColor){
            comboBox2.addItem(str);
        }

        // 查数据库获取所有类别
        set = db.select("select * from category", objs);
        try {
            while (set.next()) {
                catCategory.add(set.getString("category"));
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }

        for(String str : catCategory){
            comboBox1.addItem(str);
        }

        // 查数据库获取所有性格
        set = db.select("select * from personality", objs);
        try {
            while (set.next()) {
                catPersonality.add(set.getString("personality"));
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
        for(String str : catPersonality){
            comboBox4.addItem(str);
        }

        db.closeConnection();



        Button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(NameField.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"添加失败!\n请输入猫猫昵称!","提示",
                            JOptionPane.ERROR_MESSAGE);
                }
                else{
                    DBUtil db = new DBUtil();
                    Object[] obj = {null,
                            NameField.getText(),
                            comboBox1.getSelectedIndex() + 1,
                            comboBox2.getSelectedIndex() + 1,
                            comboBox3.getSelectedIndex() + 1,
                            comboBox4.getSelectedIndex() + 1
                    };
                    int res = db.update("insert into cat values(?,?,?,?,?,?)", obj);
                    db.closeConnection();


                    if(res!=0){
                        JOptionPane.showMessageDialog(null,"添加成功!","提示",
                                JOptionPane.PLAIN_MESSAGE);
                    }
                    else{
                        JOptionPane.showMessageDialog(null,"添加失败!","提示",
                                JOptionPane.ERROR_MESSAGE);
                    }
                    NameField.setText("");
                }
            }
        });
        Button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                viewChange = 3;
                synchronized (Main.object){
                    Main.object.notify();
                }
            }
        });
    }
}
